package twilight.companion.client.renderer;

import com.mojang.blaze3d.vertex.PoseStack;
import com.mojang.math.Axis;
import net.minecraft.client.renderer.MultiBufferSource;
import net.minecraft.client.renderer.block.BlockRenderDispatcher;
import net.minecraft.client.renderer.entity.EntityRenderer;
import net.minecraft.client.renderer.entity.EntityRendererProvider;
import net.minecraft.client.renderer.texture.OverlayTexture;
import net.minecraft.resources.ResourceLocation;
import net.minecraft.world.level.block.Blocks;
import net.minecraft.world.level.block.state.BlockState;
import twilight.companion.entity.RotatingSnowBlockEntity;

public class RotatingSnowBlockRenderer extends EntityRenderer<RotatingSnowBlockEntity> {
    private final BlockRenderDispatcher blockRenderer;
    
    public RotatingSnowBlockRenderer(EntityRendererProvider.Context context) {
        super(context);
        this.blockRenderer = context.getBlockRenderDispatcher();
    }
    
    @Override
    public void render(RotatingSnowBlockEntity entity, float entityYaw, float partialTicks, PoseStack poseStack, MultiBufferSource buffer, int packedLight) {
        poseStack.pushPose();

        BlockState snowBlockState = Blocks.SNOW_BLOCK.defaultBlockState();

        float rotationSpeed = entity.getRotationSpeed();
        float currentRotation = entity.getCurrentRotation() + rotationSpeed * partialTicks;

        poseStack.mulPose(Axis.YP.rotationDegrees(currentRotation));
        poseStack.mulPose(Axis.XP.rotationDegrees(currentRotation * 0.7F));
        poseStack.mulPose(Axis.ZP.rotationDegrees(currentRotation * 0.5F));

        float scale = 0.8F + (float)Math.sin(entity.tickCount * 0.1F) * 0.1F;
        poseStack.scale(scale, scale, scale);

        poseStack.translate(-0.5, -0.5, -0.5);

        blockRenderer.renderSingleBlock(snowBlockState, poseStack, buffer, packedLight, OverlayTexture.NO_OVERLAY);
        
        poseStack.popPose();
        super.render(entity, entityYaw, partialTicks, poseStack, buffer, packedLight);
    }
    
    @Override
    public ResourceLocation getTextureLocation(RotatingSnowBlockEntity entity) {
        return new ResourceLocation("minecraft", "textures/block/snow.png");
    }
}